package org.jeecg.modules.middleware.rabbitmq.delay;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 延时队列，需要安装延时插件配合使用
 */
@Configuration
public class DelayRabbitMqConfig {

    //通用延时队列交换机
    @Bean
    public CustomExchange genDelayExchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DelayMqConst.GEN_DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, args);
    }

    //region 通用延时队列

    //通用延时队列
    @Bean
    public Queue genDelayQueue() {
        return new Queue(DelayMqConst.GEN_DELAYED_QUEUE_NAME, true);
    }

    //给延时队列绑定交换机
    @Bean
    public Binding delayedQueueBindingDelayedExchange(Queue genDelayQueue,CustomExchange genDelayExchange) {
        return BindingBuilder.bind(genDelayQueue).to(genDelayExchange).with(DelayMqConst.GEN_DELAYED_ROUTING_KEY).noargs();
    }

    //endregion


    //region 通联订单过期队列
    @Bean
    public Queue tlDelayQueue() {
        return new Queue(DelayMqConst.TL_DELAYED_QUEUE_NAME, true);
    }

    //给延时队列绑定交换机
    @Bean
    public Binding tlDelayedQueueBindingDelayedExchange(Queue tlDelayQueue,CustomExchange genDelayExchange) {
        return BindingBuilder.bind(tlDelayQueue).to(genDelayExchange).with(DelayMqConst.TL_DELAYED_ROUTING_KEY).noargs();
    }

    //endregion
}
